﻿using System.Windows;
using System.Windows.Controls;


namespace MPE
{
    public partial class Colors : Window
    {

        Properties.Settings param = Properties.Settings.Default;
        FunkDelegate func; public delegate void FunkDelegate(object sender, FastColoredTextBoxNS.TextChangedEventArgs e);
        public Colors(FunkDelegate func)
        {
            InitializeComponent(); this.func = func;

            if (param.selectedTheme) DarkTheme.IsChecked = true; else WhiteTheme.IsChecked = true;
        }
        void SetDefault_Click(object sender, RoutedEventArgs e)
        {
            if (param.selectedTheme)
            {//DarkTheme
                DataHelper.setDarkColors(); //if (param.darkTheme == null) param.darkTheme = new System.Collections.Specialized.StringCollection(); param.darkTheme.Clear(); param.darkTheme.Add(BackColorTB.Text = "#232629"); param.darkTheme.Add(ForeColorTB.Text = "#FFF"); param.darkTheme.Add(LineNumberColorTB.Text = "#FFF"); param.darkTheme.Add(IndentBackColorTB.Text = "#232629"); param.darkTheme.Add(SelectionColorTB.Text = "#00FFFF"); param.darkTheme.Add(bracketsTB.Text = "#D26922"); param.darkTheme.Add(commandTB.Text = "#20B2AA"); param.darkTheme.Add(varableTB.Text = "#FF2E2C"); param.darkTheme.Add(keywordsTB.Text = "#EEC900"); param.darkTheme.Add(commentTB.Text = "#00FF10"); param.darkTheme.Add(userVarableTB.Text = "#FF2E2C"); param.darkTheme.Add(codeMapBackgroundTB.Text = "#000"); param.darkTheme.Add(changedLineColorTB.Text = "#E89F0E"); param.darkTheme.Add(caretColorTB.Text = "#FFF"); param.darkTheme.Add(currLineColorTB.Text = "#FFF");
            }
            else
            {//LightTheme
                DataHelper.setLightsColors();//if (param.lightTheme == null) param.lightTheme = new System.Collections.Specialized.StringCollection(); param.lightTheme.Clear(); param.lightTheme.Add(BackColorTB.Text = "#FFF"); param.lightTheme.Add(ForeColorTB.Text = "#000"); param.lightTheme.Add(LineNumberColorTB.Text = "#000"); param.lightTheme.Add(IndentBackColorTB.Text = "#FFF"); param.lightTheme.Add(SelectionColorTB.Text = "#FF0000"); param.lightTheme.Add(bracketsTB.Text = "#D26922"); param.lightTheme.Add(commandTB.Text = "#20B2AA"); param.lightTheme.Add(varableTB.Text = "#FF2E2C"); param.lightTheme.Add(keywordsTB.Text = "#EEC900"); param.lightTheme.Add(commentTB.Text = "#545656"); param.lightTheme.Add(userVarableTB.Text = "#FF2E2C"); param.lightTheme.Add(codeMapBackgroundTB.Text = "#fff"); param.lightTheme.Add(changedLineColorTB.Text = "#E89F0E"); param.lightTheme.Add(caretColorTB.Text = "#FFF"); param.lightTheme.Add(currLineColorTB.Text = "#FFF");
            }

            DataHelper.SetColorsForAllTabs();
            func(null, null);
        }
        void TB_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            TextBox tb = (sender as TextBox);

            if (new System.Text.RegularExpressions.Regex("#").Matches(tb.Text).Count > 1 || tb.Text.IndexOf("#") > 0)
            { tb.Text = tb.Text.Replace("#", ""); tb.Text = tb.Text.Insert(0, "#"); }

            for (int i = 0; i < tb.Text.Length; i++)
                if (!System.Text.RegularExpressions.Regex.IsMatch(tb.Text[i] + "", @"[#0-9a-fA-F]")) tb.Text = tb.Text.Replace(tb.Text[i] + "", "");
        }
        void TB_LostFocus(object sender, RoutedEventArgs e) { TextBox tb = (sender as TextBox); if (tb.Text.Length < 4) tb.Text = "#000"; }
        void Exec_Click(object sender, RoutedEventArgs e)
        {
            if (DarkTheme.IsChecked == true)
            {
                if (param.darkTheme == null) param.darkTheme = new System.Collections.Specialized.StringCollection();
                param.darkTheme.Clear();


                param.darkTheme.Add(BackColorTB.Text);
                param.darkTheme.Add(ForeColorTB.Text);
                param.darkTheme.Add(LineNumberColorTB.Text);
                param.darkTheme.Add(IndentBackColorTB.Text);
                param.darkTheme.Add(SelectionColorTB.Text);


                param.darkTheme.Add(bracketsTB.Text);
                param.darkTheme.Add(commandTB.Text);
                param.darkTheme.Add(varableTB.Text);
                param.darkTheme.Add(keywordsTB.Text);
                param.darkTheme.Add(commentTB.Text);
                param.darkTheme.Add(userVarableTB.Text);
                param.darkTheme.Add(codeMapBackgroundTB.Text);
                param.darkTheme.Add(changedLineColorTB.Text);
                param.darkTheme.Add(caretColorTB.Text);
                param.darkTheme.Add(currLineColorTB.Text);
            }
            else
            {
                if (param.lightTheme == null) param.lightTheme = new System.Collections.Specialized.StringCollection();
                param.lightTheme.Clear();


                param.lightTheme.Add(BackColorTB.Text);
                param.lightTheme.Add(ForeColorTB.Text);
                param.lightTheme.Add(LineNumberColorTB.Text);
                param.lightTheme.Add(IndentBackColorTB.Text);
                param.lightTheme.Add(SelectionColorTB.Text);


                param.lightTheme.Add(bracketsTB.Text);
                param.lightTheme.Add(commandTB.Text);
                param.lightTheme.Add(varableTB.Text);
                param.lightTheme.Add(keywordsTB.Text);
                param.lightTheme.Add(commentTB.Text);
                param.lightTheme.Add(userVarableTB.Text);
                param.lightTheme.Add(codeMapBackgroundTB.Text);
                param.lightTheme.Add(changedLineColorTB.Text);
                param.lightTheme.Add(caretColorTB.Text);
                param.lightTheme.Add(currLineColorTB.Text);
            }
            param.Save();
            DataHelper.SetColorsForAllTabs();
            func(null, null);
        }
        void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) => DragMove();
        void Theme_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).Content.ToString() == "Темная")// Нажали на тёмную тему
                param.selectedTheme = true;

            else// Нажали на светлую тему
                param.selectedTheme = false;

            param.Save();

            BackColorTB.Text = DataHelper.getThemeColor(0);
            ForeColorTB.Text = DataHelper.getThemeColor(1);
            LineNumberColorTB.Text = DataHelper.getThemeColor(2);
            IndentBackColorTB.Text = DataHelper.getThemeColor(3);
            SelectionColorTB.Text = DataHelper.getThemeColor(4);


            bracketsTB.Text = DataHelper.getThemeColor(5);
            commandTB.Text = DataHelper.getThemeColor(6);
            varableTB.Text = DataHelper.getThemeColor(7);
            keywordsTB.Text = DataHelper.getThemeColor(8);
            commentTB.Text = DataHelper.getThemeColor(9);
            userVarableTB.Text = DataHelper.getThemeColor(10);
            codeMapBackgroundTB.Text = DataHelper.getThemeColor(11);
            changedLineColorTB.Text = DataHelper.getThemeColor(12);
            caretColorTB.Text = DataHelper.getThemeColor(13);
            currLineColorTB.Text = DataHelper.getThemeColor(14);

            DataHelper.SetColorsForAllTabs();
        }
    }
}